package top.xinsix.est.service.impl;
import org.springframework.beans.BeanUtils;
import top.xinsix.est.dto.EvaluateDetailDto;
import top.xinsix.est.dto.EvaluateDto;
import top.xinsix.est.dto.ModelDto;
import top.xinsix.est.mapper.EvaluateDetailMapper;
import top.xinsix.est.mapper.EvaluateMapper;
import top.xinsix.est.mapper.ModelMapper;
import top.xinsix.est.pojo.Evaluate;
import top.xinsix.est.pojo.EvaluateDetail;
import top.xinsix.est.pojo.Model;
import top.xinsix.est.service.EvaluateService;
import org.springframework.stereotype.Service;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import top.xinsix.est.dto.Page;
import top.xinsix.est.constants.Constants;
import top.xinsix.est.tools.EmptyUtils;

import javax.annotation.Resource;

@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Resource
    private EvaluateMapper evaluateMapper;

    @Resource
    private ModelMapper modelMapper;
    @Resource
    private EvaluateDetailMapper detailMapper;

    public Evaluate getEvaluateById(Long id){
        return evaluateMapper.getEvaluateById(id);
    }

    public List<Evaluate>	getEvaluateListByMap(Map<String,Object> param){
        return evaluateMapper.getEvaluateListByMap(param);
    }

    public Integer getEvaluateCountByMap(Map<String,Object> param){
        return evaluateMapper.getEvaluateCountByMap(param);
    }

    public Integer qdtxAddEvaluate(Evaluate evaluate){
            evaluate.setCreatdTime(new Date());
            return evaluateMapper.insertEvaluate(evaluate);
    }

    public Integer qdtxModifyEvaluate(Evaluate evaluate){
        evaluate.setUpdatedTime(new Date());
        return evaluateMapper.updateEvaluate(evaluate);
    }

    public Integer qdtxDeleteEvaluateById(Long id){
        return evaluateMapper.deleteEvaluateById(id);
    }

    public Integer qdtxBatchDeleteEvaluate(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return evaluateMapper.batchDeleteEvaluate(param);
    }

    public Page<Evaluate> queryEvaluatePageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = evaluateMapper.getEvaluateCountByMap(param);
        pageNo = EmptyUtils.isEmpty(pageNo) ? Constants.DEFAULT_PAGE_NO : pageNo;
        pageSize = EmptyUtils.isEmpty(pageSize) ? Constants.DEFAULT_PAGE_SIZE : pageSize;
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<Evaluate> evaluateList = evaluateMapper.getEvaluateListByMap(param);
        page.setList(evaluateList);
        return page;
     }

    /**
     * 查看评估详情信息
     * @param id 评估ID
     * @return
     */
    public EvaluateDto assessInfo(Long id) {
        Evaluate evaluate = evaluateMapper.getEvaluateById(id);
        EvaluateDto evaluateDto = new EvaluateDto(evaluate);
        //组装数据
        //获取型号（model），并将他的值赋给evaluateDto对象
        //根据商品型号获取型号信息（model）
        Model model = modelMapper.getModelById(evaluate.getModelId());
        ModelDto modelDto = new ModelDto(model);
        evaluateDto.setModel(modelDto);
        //根据评估ID查询评估详情列表
        Map<String,Object> params = new HashMap<String, Object>();
        params.put("evaluateId",evaluate.getId());
        List<EvaluateDetail> detailsList = detailMapper.getEvaluateDetailListByMap(params);
        List<EvaluateDetailDto> detailDtoList = new ArrayList<EvaluateDetailDto>();
        //组装数据
        for (EvaluateDetail detail:detailsList){
            EvaluateDetailDto detailDto = new EvaluateDetailDto();
            BeanUtils.copyProperties(detail,detailDto);
            detailDtoList.add(detailDto);
        }
        evaluateDto.setDetailList(detailDtoList);
        return evaluateDto;
    }
}
